//
//  HLContactListVC.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/19.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLContactListVC.h"
#import "HLUnderlineView.h"
#import "UITextField+function.h"
#import "UITableView+Function.h"
#import "HLConsigneeObject.h"
#import "HLConsignorObject.h"

@interface HLContactListVC ()
<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,assign)ContactType contactType;
@property(nonatomic,strong)UITextField* textFieldSearch;
@property(nonatomic,strong)UITableView* tableViewContact;
@property(nonatomic,strong)NSMutableArray* arrayContacts;

@end

@implementation HLContactListVC

@synthesize onSelectContact;

- (instancetype)initWithType:(ContactType)type {
    self = [super init];
    if (self) {
        self.contactType = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arrayContacts = [NSMutableArray new];
    
    HLUnderlineView* view = [[HLUnderlineView alloc]init];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.height.mas_equalTo(60);
    }];
    
    [view addSubview:self.textFieldSearch];
    [self.textFieldSearch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(view).insets(UIEdgeInsetsMake(10, 15, 10, 15));
    }];
    
    if (self.contactType == sender) {
        [self setNavigationTitle:@"选择托运人" Color:[UIColor whiteColor]];
        self.textFieldSearch.placeholder = @"请输入托运人姓名或手机号";
    } else {
        [self setNavigationTitle:@"选择收货人" Color:[UIColor whiteColor]];
        self.textFieldSearch.placeholder = @"请输入收货人人姓名或手机号";
    }
    
    [self.view addSubview:self.tableViewContact];
    [self.tableViewContact mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(view.mas_bottom);
    }];
    [self.tableViewContact.mj_header beginRefreshing];
}

- (void)onClickSearch {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getContactList {
    [HLWebManager getContactList:self.contactType name:@"" telephone:@"" successBlock:^(id response) {
        [self.tableViewContact.mj_header endRefreshing];
        if ([response[@"code"]integerValue]==1) {
            [self.arrayContacts removeAllObjects];
            if (self.contactType == sender) {
                [self.arrayContacts addObjectsFromArray:[HLConsignorObject mj_objectArrayWithKeyValuesArray:response[@"myConsignorList"]]];
            } else {
                [self.arrayContacts addObjectsFromArray:[HLConsigneeObject mj_objectArrayWithKeyValuesArray:response[@"myConsigneeList"]]];
            }
            [self.tableViewContact reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failureBlock:^(NSError *error) {
        [self.tableViewContact.mj_header endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayContacts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HLContactCell* cell = [tableView dequeueReusableCellWithIdentifier:@"HLContactCell"];
    if (!cell) {
        cell = [[HLContactCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HLContactCell"];
    }
    if (self.contactType == sender) {
        HLConsignorObject* model = self.arrayContacts[indexPath.row];
        cell.consignor = model;
    } else {
        HLConsigneeObject* model = self.arrayContacts[indexPath.row];
        cell.consignee = model;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    id model = nil;
    if (self.contactType == sender) {
        model = (HLConsignorObject* )self.arrayContacts[indexPath.row];
    } else {
        model = (HLConsigneeObject* )self.arrayContacts[indexPath.row];
    }
    if (onSelectContact) {
        onSelectContact(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITextField*)textFieldSearch {
    if (!_textFieldSearch) {
        _textFieldSearch = [[UITextField alloc]init];
        UIButton* btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
        btnSearch.frame = CGRectMake(0, 0, 40, 20);
        [btnSearch setImage:[UIImage imageNamed:@"hl_home_search"] forState:UIControlStateNormal];
        [btnSearch addTarget:self action:@selector(onClickSearch) forControlEvents:UIControlEventTouchUpInside];
        btnSearch.imageView.contentMode = UIViewContentModeCenter;
        btnSearch.contentHorizontalAlignment= UIControlContentHorizontalAlignmentCenter;
        btnSearch.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
        _textFieldSearch.rightView = btnSearch;
        _textFieldSearch.rightViewMode = UITextFieldViewModeAlways;
        _textFieldSearch.layer.borderColor = [UIColor grayColor].CGColor;
        _textFieldSearch.layer.borderWidth = 0.6;
        _textFieldSearch.layer.cornerRadius = 25;
        [_textFieldSearch setLeftInset:15];
    }
    return _textFieldSearch;
}

- (UITableView*)tableViewContact {
    if (!_tableViewContact) {
        _tableViewContact = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableViewContact.dataSource = self;
        _tableViewContact.delegate = self;
        _tableViewContact.hideExtraCell = YES;
        _tableViewContact.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getContactList)];
    }
    return _tableViewContact;
}

@end
